package com.umeox.watch.moto.chat.adapter

import android.annotation.SuppressLint
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.umeox.moto.watch.themes.widget.RoundedImageView
import com.umeox.moto.watch.themes.widget.TextView
import com.umeox.watch.moto.chat.R
import com.umeox.watch.moto.chat.base.GlideApp
import com.umeox.watch.moto.chat.bean.DeleteGroupMemberInfo

class DeleteFriendSelectListAdapter : RecyclerView.Adapter<DeleteFriendSelectListAdapter.Holder>() {

    private val deviceFriendInfoList = ArrayList<DeleteGroupMemberInfo>()

    private var onItemClickListener: OnItemClickListener? = null

    @SuppressLint("NotifyDataSetChanged")
    fun setData(list: List<DeleteGroupMemberInfo>) {
        if (list.isEmpty()) {
            return
        }
        deviceFriendInfoList.clear()
        deviceFriendInfoList.addAll(list)
        notifyDataSetChanged()
    }

    fun getSelectedFriendIds():List<String> {
        return deviceFriendInfoList.filter {
            it.selected
        }.map {
            it.contactInfo.memberId
        }
    }

    fun setOnItemClickListener(listener: OnItemClickListener?) {
        onItemClickListener = listener
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_friends_list, parent, false)
        return Holder(view)
    }

    override fun onBindViewHolder(holder: Holder, position: Int) {
        holder.bind(position, deviceFriendInfoList[position])
    }

    override fun getItemCount(): Int {
        return deviceFriendInfoList.size
    }

    inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val ivAvatar: RoundedImageView =
            itemView.findViewById(R.id.ivAvatar)
        private val tvName: TextView =
            itemView.findViewById(R.id.tvName)
        private val ivSelected: ImageView =
            itemView.findViewById(R.id.ivSelected)


        @SuppressLint("SetTextI18n")
        fun bind(position: Int, deleteGroupMemberInfo: DeleteGroupMemberInfo) {
            tvName.text = deleteGroupMemberInfo.contactInfo.name
            if (TextUtils.isEmpty(deleteGroupMemberInfo.contactInfo.avatar)) {
                GlideApp.with(itemView.context)
                    .load(R.drawable.default_avatar)
                    .apply(RequestOptions.bitmapTransform(CircleCrop()))
                    .into(ivAvatar)
            } else {
                GlideApp.with(itemView.context)
                    .load(deleteGroupMemberInfo.contactInfo.avatar)
                    .apply(RequestOptions.bitmapTransform(CircleCrop()))
                    .into(ivAvatar)
            }
            if (deleteGroupMemberInfo.available) {
                if (deleteGroupMemberInfo.selected) {
                    ivSelected.setImageLevel(0)
                } else {
                    ivSelected.setImageLevel(1)
                }
                itemView.setOnClickListener {
                    onItemClickListener?.onItemClick(position, deleteGroupMemberInfo)
                }
            } else {
                ivSelected.setImageLevel(2)
                itemView.setOnClickListener {
                }
            }
        }
    }

    interface OnItemClickListener {
        fun onItemClick(position: Int, data: DeleteGroupMemberInfo)
    }
}